using System.Net.Http;
using System.Web.Http;
using AutoMapper;
using Common.Enumerations;
using Dto.ApiRequests;
using NServiceBus;
using Sanelib.Dto;
using WebApp.Services;

namespace WebApp.ControllerApis
{
    [Authorize]
    public class ReportRequestController : SmartApiController
    {
        public IBus Bus { get; set; }

        public ReportRequestController(IUserSession userSession, IMappingEngine mappingEngine)
            : base(userSession, mappingEngine)
        {
        }

        public HttpResponseMessage Post(ReportRequestForm form)
        {
            var response = new WebApiResultResponse();
            
            Bus.Send<NSBus.Dto.Commands.GenerateSimpleExportCommand>(c =>
            {
                c.UserId = GetCurrentUser().Id;
                c.SearchSpecification = form;
                c.ViewType = "Common.Views." + EntityType.FromValue(form.EntityTypeValue).DisplayName + "View, Common";
            });

            response.IsSuccess = true;
            return Content(response);
        }
    }
}